package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.IApplication;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.R.id;
import cn.wikiflyer.beikang.bean.AdBeanResult;
import cn.wikiflyer.beikang.bean.NoticeBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.utils.WKTimeUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 我的通知详情
 * 
 * @author qunlee_mr
 */
public class UserNoticeInfoAct extends BaseActivity {

	@ViewInject(id = id.noticeinfo_title)
	private TextView title;

	@ViewInject(id = id.noticeinfo_time)
	private TextView time;

	@ViewInject(id = id.noticeinfo_content)
	private TextView content;

	private NoticeBean bean = new NoticeBean();

	@Override
	public void getIntentData(Bundle savedInstanceState) {
		super.getIntentData(savedInstanceState);
	}

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_notice_info);
	}

	@Override
	public void init() {
		bean = (NoticeBean) getIntent().getExtras().getSerializable(
				"NoticeBean");
		new GetReadNoticeTask(ctx);
	}

	@Override
	public void setData() {
		updateUI();
	}

	private void updateUI() {
		String[] type = { "系统公告", "好友消息" };
		String tStr = type[bean.send_from_id.equals("admin") ? 0 : 1];
		getHeader().setTitle("" + tStr);
		title.setText(bean.subject);
		time.setText(WKTimeUtils.getTimeFromLongEndWithMM(Long
				.valueOf(bean.message_time) * 1000));
		content.setText(bean.content);
	}

	/**读取通知线程
	 */
	class GetReadNoticeTask extends WKAsyncTaskPro {
		public GetReadNoticeTask(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... params) {
			return app().net.readNotice(bean.messageid);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean<NoticeBean>>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean<NoticeBean> b = gson.fromJson(result, cvbType);
			if (b.status == 1) {
				updateUI();
			} else {
				IApplication.showToast(b.info);
			}
		}
	}

}
